package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.ElasticsearchQueryBuilderService;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.WarningRuleMapper;
import com.ruoyi.system.service.IWarningRuleService;

/**
 * 预警规则Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-06-19
 */
@Service
public class WarningRuleServiceImpl implements IWarningRuleService 
{
    @Autowired
    private WarningRuleMapper warningRuleMapper;
    @Autowired
    ElasticsearchQueryBuilderService elasticsearchQueryBuilderService;
    @Autowired
    private EsUtil esUtil;
    /**
     * 查询预警规则
     * 
     * @param id 预警规则ID
     * @return 预警规则
     */
    @Override
    public WarningRule selectWarningRuleById(Long id)
    {
        WarningRule rule= warningRuleMapper.selectWarningRuleById(id);
        return getWarningRuleData(rule);
    }

    /**
     * 查询预警规则列表
     * 
     * @param warningRule 预警规则
     * @return 预警规则
     */
    @Override
//    @DataScope(deptAlias = "d",userAlias = "u")
    public List<WarningRule> selectWarningRuleList(WarningRule warningRule)
    {
        List<WarningRule> list=warningRuleMapper.selectWarningRuleList(warningRule);
        for (WarningRule rule:list) {
            getWarningRuleData(rule);
        }
        return list;
    }

    /**
     * 新增预警规则
     * 
     * @param warningRule 预警规则
     * @return 结果
     */
    @Override
    public int insertWarningRule(WarningRule warningRule)
    {
        //默认启用
        warningRule.setEnabledState(1);
        warningRule.setIsSystem(0);
        int res =warningRuleMapper.insertWarningRule(warningRule);
        insertRuleEmotion(warningRule);
        insertRuleMediaType(warningRule);
        insertRuleReceiver(warningRule);
        insertRuleWarningNoticeType(warningRule);
        return res;
    }

    /**
     * 修改预警规则
     * 
     * @param warningRule 预警规则
     * @return 结果
     */
    @Override
    public int updateWarningRule(WarningRule warningRule)
    {
        //删除关联数据
        deleteRuleReceiverByRuleId(warningRule.getId());
        deleteRuleMediaTypeByRuleId(warningRule.getId());
        deleteRuleEmotionByRuleId(warningRule.getId());
        deleteRuleWarningNoticeTypeByRuleId(warningRule.getId());
        //增加关联数据
        if(warningRule.getEmotion()!=null){
            insertRuleEmotion(warningRule);
        }
        if(warningRule.getMediaType()!=null){
            insertRuleMediaType(warningRule);
        }
        if(warningRule.getReceiverList()!=null) {
            insertRuleReceiver(warningRule);
        }
        if(warningRule.getWarningNoticeType()!=null) {
            insertRuleWarningNoticeType(warningRule);
        }
        //修改主数据
        return warningRuleMapper.updateWarningRule(warningRule);
    }

    /**
     * 批量删除预警规则
     * 
     * @param ids 需要删除的预警规则ID
     * @return 结果
     */
    @Override
    public int deleteWarningRuleByIds(Long[] ids)
    {
        warningRuleMapper.deleteRuleReceiver(ids);
        warningRuleMapper.deleteRuleWarningNoticeType(ids);
        warningRuleMapper.deleteRuleMediaType(ids);
        warningRuleMapper.deleteRuleEmotion(ids);
        return warningRuleMapper.deleteWarningRuleByIds(ids);
    }

    /**
     * 删除预警规则信息
     * 
     * @param id 预警规则ID
     * @return 结果
     */
    @Override
    public int deleteWarningRuleById(Long id)
    {
        deleteRuleReceiverByRuleId(id);
        deleteRuleWarningNoticeTypeByRuleId(id);
        deleteRuleMediaTypeByRuleId(id);
        deleteRuleEmotionByRuleId(id);
        return warningRuleMapper.deleteWarningRuleById(id);
    }

    @Override
    public int deleteRuleReceiverByRuleId(Long id) {
        return warningRuleMapper.deleteRuleReceiverByRuleId(id);
    }

    @Override
    public int deleteRuleMediaTypeByRuleId(Long id) {
        return warningRuleMapper.deleteRuleMediaTypeByRuleId(id);
    }

    @Override
    public int deleteRuleEmotionByRuleId(Long id) {
        return warningRuleMapper.deleteRuleEmotionByRuleId(id);
    }

    @Override
    public int deleteRuleWarningNoticeTypeByRuleId(Long id) {
        return warningRuleMapper.deleteRuleWarningNoticeTypeByRuleId(id);
    }

    @Override
    public int insertRuleReceiver(WarningRule warningRule) {
        int rows = 1;
        // 新增规则与接收者绑定
        List<RuleReceiver> list = new ArrayList<>();
        for (Long receiverId : warningRule.getReceiverList())
        {
            RuleReceiver rm = new RuleReceiver();
            rm.setRuleId(warningRule.getId());
            rm.setReceiver(receiverId);
            list.add(rm);
        }
        if (list.size() > 0)
        {
            rows = warningRuleMapper.batchRuleReceiver(list);
        }
        return rows;
    }

    @Override
    public int insertRuleWarningNoticeType(WarningRule warningRule) {
        int rows = 1;
        // 新增规则与情感特征绑定
        List<RuleWarningNoticeType> list = new ArrayList<>();
        for (String warningNoticeType : warningRule.getWarningNoticeType())
        {
            RuleWarningNoticeType rm = new RuleWarningNoticeType();
            rm.setRuleId(warningRule.getId());
            rm.setWarningNoticeType(warningNoticeType);
            list.add(rm);
        }
        if (list.size() > 0)
        {
            rows = warningRuleMapper.batchRuleWarningNoticeType(list);
        }
        return rows;
    }

    @Override
    public int insertRuleMediaType(WarningRule warningRule) {
        int rows = 1;
        // 新增规则与情感特征绑定
        List<RuleMediaType> list = new ArrayList<>();
        for (String mediaType : warningRule.getMediaType())
        {
            RuleMediaType rm = new RuleMediaType();
            rm.setRuleId(warningRule.getId());
            rm.setMediaType(mediaType);
            list.add(rm);
        }
        if (list.size() > 0)
        {
            rows = warningRuleMapper.batchRuleMediaType(list);
        }
        return rows;
    }

    @Override
    public int insertRuleEmotion(WarningRule warningRule) {
        int rows = 1;
        // 新增规则与情感特征绑定
        List<RuleEmotion> list = new ArrayList<>();
        for (String emotion : warningRule.getEmotion())
        {
            RuleEmotion rm = new RuleEmotion();
            rm.setRuleId(warningRule.getId());
            rm.setEmotion(emotion);
            list.add(rm);
        }
        if (list.size() > 0)
        {
            rows = warningRuleMapper.batchRuleEmotion(list);
        }
        return rows;
    }

    @Override
    public List<RuleReceiver> getReceiverByruleId(Long id) {
        return warningRuleMapper.getReceiverByruleId(id);
    }

    @Override
    public List<RuleWarningNoticeType> getWarningNoticeTypeByruleId(Long id) {
        return warningRuleMapper.getWarningNoticeTypeByruleId(id);
    }

    @Override
    public WarningRule getWarningRuleData(WarningRule rule) {
        List<RuleReceiver> receivers=  getReceiverByruleId(rule.getId());
        List<RuleWarningNoticeType> types=  getWarningNoticeTypeByruleId(rule.getId());
        rule.setReceivers(receivers);
        rule.setWarningTypes(types);
        StringBuffer sb=new StringBuffer();
        receivers.forEach(receiver -> {
            if(sb.length()==0){
                sb.append(receiver.getReceiverName());
            }else{
                sb.append(","+receiver.getReceiverName());
            }
        });
        rule.setWarningNoticeUsername(sb.toString());

        StringBuffer sp=new StringBuffer();
        types.forEach(type -> {
            if(sp.length()==0){
                sp.append(type.getWarningNoticeTypeName());
            }else{
                sp.append(","+type.getWarningNoticeTypeName());
            }

        });
        rule.setWarningNoticeContent(sp.toString());

       List<String> emotions= getEmotionByRuleId(rule.getId());
       rule.setEmotion(emotions);

        List<String> mediaTypes= getMediaTypeByRuleId(rule.getId());
        rule.setMediaType(mediaTypes);


        return rule;
    }

    @Override
    public List<PoPublicOpinion> getOpinionsByRule(WarningRule rule) {
        BoolQueryBuilder queryBuilder= elasticsearchQueryBuilderService.boolQueryBuilder(rule);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(queryBuilder);
        try {
            return esUtil.search(searchSourceBuilder, PoPublicOpinion.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<>();

    }

    private List<String> getMediaTypeByRuleId(Long id) {
        return warningRuleMapper.getMediaTypeByRuleId(id);
    }


    private List<String> getEmotionByRuleId(Long id) {
        return warningRuleMapper.getEmotionByRuleId(id);
    }


}
